package com.star.service.impl;

import com.star.constant.StatusConstant;
import com.star.entity.Dish;
import com.star.entity.Orders;
import com.star.entity.Setmeal;
import com.star.mapper.DishMapper;
import com.star.mapper.OrderMapper;
import com.star.mapper.SetmealMapper;
import com.star.mapper.UserMapper;
import com.star.service.WorkspaceService;
import com.star.vo.business.BusinessDataVO;
import com.star.vo.DishOverViewVO;
import com.star.vo.OrderOverViewVO;
import com.star.vo.SetmealOverViewVO;
import com.star.vo.admin.OperationalDataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查询系统今日运营数据
     * @param begin
     * @param end
     * @return
     */
    public OperationalDataVO getOperationalData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("beginTime", begin);
        map.put("endTime", end);
        // 当天新增用户
        Integer newUsers = userMapper.countByMap(map);
        // 当天订单数
        Integer orderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        // 当天营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover != null ? turnover : 0.0;
        // 当天有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        // 订单完成率
        double orderCompletionRate = 0.0;
        // 平均客单价
        double unitPrice = 0.0;
        if(orderCount != 0 && validOrderCount != 0){
            orderCompletionRate =  (double) validOrderCount / orderCount;
            unitPrice = turnover / validOrderCount;
        }

        return OperationalDataVO
                .builder()
                .newUsers(newUsers)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 查询今日运营数据
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end, Long businessId) {
        Map map = new HashMap();
        map.put("beginTime", begin);
        map.put("endTime", end);
        map.put("businessId", businessId);
        // 当天订单数
        Integer orderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        // 当天营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover != null ? turnover : 0.0;
        // 当天有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        // 订单完成率
        double orderCompletionRate = 0.0;
        // 平均客单价
        double unitPrice = 0.0;
        if(orderCount != 0 && validOrderCount != 0){
            orderCompletionRate =  (double) validOrderCount / orderCount;
            unitPrice = turnover / validOrderCount;
        }

        return BusinessDataVO
                .builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO getOverviewDishes(Long businessId) {
        Dish dish = new Dish();
        dish.setBusinessId(businessId);
        // 停售
        dish.setStatus(StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByStatus(dish);
        // 起售
        dish.setStatus(StatusConstant.ENABLE);
        Integer sold = dishMapper.countByStatus(dish);
        return DishOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO getOverviewSetmeals(Long businessId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setBusinessId(businessId);
        // 停售
        setmeal.setStatus(StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByStatus(setmeal);
        // 起售
        setmeal.setStatus(StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByStatus(setmeal);
        return SetmealOverViewVO
                .builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO getOverviewOrders(Long businessId) {
        Orders orders = new Orders();
        orders.setBusinessId(businessId);
        // 全部订单
        Integer allOrders = orderMapper.countByAll(orders);
        // 已取消数量
        orders.setStatus(Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByStatus(orders);
        // 已完成数量
        orders.setStatus(Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByStatus(orders);
        // 待派送数量
        orders.setStatus(Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByStatus(orders);
        // 待接单数量
        orders.setStatus(Orders.REFUND);
        Integer waitingOrders = orderMapper.countByStatus(orders);
        return OrderOverViewVO
                .builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
